package com.lowca.news.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.compass.core.util.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.PropertyProjection;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Subqueries;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.lowca.news.dao.UserDao;
import com.lowca.news.dao.support.Pager;
import com.lowca.news.entity.Role;
import com.lowca.news.entity.User;
import com.lowca.news.web.formbean.AdminUserConditionForm;

@Repository("userDao")
public class UserDaoImpl extends BaseDaoImpl<User, Long> implements UserDao {

	@Override
	public User findByUsername(String username) {
		Assert.hasText(username, "username is required");
		return super.findUniqueByProperty("username", username);
	}

	@Override
	public User findByEmail(String email) {
		Assert.hasText(email, "email is required");
		return super.findUniqueByProperty("email", email);
	}

	@Override
	public User findByUsernameAndPassword(String username, String password) {
		Assert.hasText(username, "username is required");
		Assert.hasText(password, "password is required");
		String hql = "from User a where a.username=? and a.password=?";
		return super.findUniqueByHQL(hql, username, password);
	}

	@Override
	public boolean existByUsername(String username) {
		Assert.hasText(username, "username is required");
		return super.existByProperty("username", username);
	}

	@Override
	public boolean existByEmail(String email) {
		Assert.hasText(email, "email is required");
		return super.existByProperty("email", email);
	}

	@Override
	public Pager<User> searchUser(AdminUserConditionForm condition, int pageNo,
			int pageSize) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer sb = new StringBuffer("select u from User u");
		if (condition != null) {
			if (StringUtils.hasText(condition.getRoleName())) {
				sb.append(" inner join u.roles r where r.name=?");
				params.add(condition.getRoleName());
			} else {
				sb.append(" where 1=1");
			}
			if (StringUtils.hasText(condition.getUsername())) {
				sb.append(" and u.username like ?");
				params.add("%" + condition.getUsername() + "%");
			}
			if (StringUtils.hasText(condition.getEmail())) {
				sb.append(" and u.email like ?");
				params.add("%" + condition.getEmail() + "%");
			}
			if (condition.getRegisterTimeMin() != null) {
				sb.append(" and u.registerTime>=?");
				params.add(condition.getRegisterTimeMin());
			}
			if (condition.getRegisterTimeMax() != null) {
				sb.append(" and u.registerTime<=?");
				params.add(condition.getRegisterTimeMax());
			}
			if (condition.getEnabled() != null) {
				sb.append(" and u.enabled=?");
				params.add(condition.getEnabled());
			}
		}
		sb.append(" order by u.registerTime desc");
		return super.findPageByHQL(pageNo, pageSize, sb.toString(),
				params.toArray());
	}
}
